﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PKURG.CostSplit.Business;
using System.DirectoryServices;

public partial class Admin_UserConfig_ManageUserAdvance : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //gvUserList.DataBind();
    }
    protected override bool CheckPermission()
    {
        return Permission.GetManagerPermission();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (ddlUserList.Items.Count==0)
        {
            lbResult.Text = "请先选择用户";
            return;
        }
        string uName = "founder\\"+ddlUserList.SelectedItem.Value.ToLower();

        int count = new PKURG.CostSplit.Services.SysUserService().Find("name=" + uName).Count;
        if (count == 0)
        {
            new PKURG.CostSplit.Services.SysUserService().Save(new PKURG.CostSplit.Entities.SysUser()
            {
                Name = uName,
                DisplayName = ddlUserList.SelectedItem.Text,
                Ptype = int.Parse(rblUtypesA.SelectedItem.Value),
                RwType = int.Parse(ddlUType0.SelectedItem.Value)
            });
            lbResult.Text = "添加成功";
        }
        else
        {
            lbResult.Text = "已存在此用户";
        }
        gvUserList.DataBind();
    }
    //protected void gvUserList_RowCommand(object sender, GridViewCommandEventArgs e)
    //{

    //}
    protected void gvUserList_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string id = e.Keys[0].ToString();

        string diplayName = ((TextBox)gvUserList.Rows[e.RowIndex].FindControl("tbDisplayName")).Text;
        string ptype = ((RadioButtonList)gvUserList.Rows[e.RowIndex].FindControl("rblUtypes")).SelectedItem.Value;
        string rwtype = ((DropDownList)gvUserList.Rows[e.RowIndex].FindControl("ddlUType")).SelectedItem.Value;

        Sys_Admin.UpdateUserInfo(id,
            diplayName,
            int.Parse(ptype),
             int.Parse(rwtype)
        );


        e.Cancel = true;
        gvUserList.EditIndex = -1;
        gvUserList.DataBind();
    }
    protected void gvUserList_RowUpdated(object sender, GridViewUpdatedEventArgs e)
    {

    }

    public string GetUserType(string rwType)
    {
        switch (rwType)
        {
            case "0":
                return "只读";
            case "1":
                return "读写";
            case "2":
                return "管理员";
            default:
                break;
        }
        return "";
    }
    protected void gvUserList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string id = e.Keys[0].ToString();
        new PKURG.CostSplit.Services.SysUserService().Delete(id);
        e.Cancel = true;
        gvUserList.DataBind();
    }
    protected void btnSearchUser_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(tbName.Text))
        {
            return;
        }
        ADHelper helper = new ADHelper();
        List<DirectoryEntry> ls = helper.GetListByAccount(tbName.Text);
        Console.WriteLine("共 {0} 项", ls.Count);
        ddlUserList.Items.Clear();
        foreach (DirectoryEntry item in ls)
        {
            if (item.Properties["sAMAccountName"].Value==null||item.Properties["Name"].Value==null)
            {
                continue;
            }
            ddlUserList.Items.Add(new ListItem()
            {
                Text =string.Format("{0}[{1}]",item.Properties["Name"].Value.ToString(),item.Properties["sAMAccountName"].Value.ToString()),
                Value = item.Properties["sAMAccountName"].Value.ToString()
            });
        }
    }

}